package cn.iocoder.springboot.labs.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: hdh
 * @Desc:
 * @create: 2024-02-27 17:42
 **/

@RestController
@RequestMapping("/session")
public class SessionController {

    @GetMapping("set")
    public void  set(HttpSession session, @RequestParam("key") String key ,@RequestParam("value") String value){
        session.setAttribute(key,value);
    }

    @GetMapping("/get_all")
    public Map<String,Object> getAll(HttpSession session){
        System.out.println(session);
        Map<String,Object> result = new HashMap<>();
        //遍历
        for (Enumeration<String> enumeration = session.getAttributeNames();
           enumeration.hasMoreElements();
        ){
            String key = enumeration.nextElement();
            Object value = session.getAttribute(key);
            result.put(key,value);

        }
        //返回
        return  result;
    }
}
